local skel = fk.CreateSkill {
  name = "emo__xunchou",
  --tags = {Skill.Compulsory},
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable{
  ["emo__xunchou"] = "寻仇",
  [":emo__xunchou"] = "非首轮开始时，你执行一个视为拥有“挑衅”的回合，调离上轮未造成过伤害的其他角色至回合结束。",
  ["@emo__xunchou-phase"] = "进步",

  ["$emo__xunchou1"] = "听说你这段时间一直在找我，没完啦？",
  ["$emo__xunchou2"] = "看来你是真想跟我拼一下了",
  ["$emo__xunchou3"] = "跟我刘华强拼你有这实力吗",
  ["$emo__xunchou4"] = "给你机会你不中用啊",
}

skel:addEffect(fk.RoundStart, {
  anim_type = "offensive",
  audio_index = {1,2},
  can_trigger = function(self, event, target, player, data)
    local round = player.room:getBanner("RoundCount")
    return player:hasSkill(skel.name) and round and round > 1
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tag = room:getBanner("emo__xunchou-round") -- 上轮造成伤害的角色
    if tag == nil then
      local players = {}
      local current = room.logic:getCurrentEvent():findParent(GameEvent.Round, true)
      if not current then return end
      local last = room.logic:getEventsByRule(GameEvent.Round, 1, function (e)
        return e.id < current.id
      end, 1)[1]
      if last then
        room.logic:getActualDamageEvents(1, function(e)
          if e.id > last.end_id then return end
          local dmg = e.data
          table.insert(players, dmg.from)
        end, nil, last.id)
      end
      room:setBanner("emo__xunchou-round", players)
    end
    player:gainAnExtraTurn(true, skel.name)
  end,
})

skel:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getCurrentExtraTurnReason() == skel.name
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local players = room:getBanner("emo__xunchou-round") -- 上轮造成伤害的角色
    if type(players) == "table" then
      if #players > 0 then
        room:doIndicate(player, players)
      end
      for _, to in ipairs(room.alive_players) do
        if not table.contains(players, to) and to ~= player then
          DIY.removePlayer(to, "-turn")
        end
      end
    end
    room:handleAddLoseSkills(player, "emo_ol_ex_tiaoxin")
    room.logic:getCurrentEvent():addCleaner(function()
      room:handleAddLoseSkills(player, "-emo_ol_ex_tiaoxin", nil, false)
    end)
  end,
})



return skel
